<template>
  <el-dialog
    v-model="visible"
    title="批量删除资产"
    width="600px"
    :close-on-click-modal="false"
  >
    <div class="batch-delete-content">
      <div class="info-section">
        <p>
          已选择 <strong>{{ selectedIds.length }}</strong> 个资产进行批量删除
        </p>
        <p class="danger-text">⚠️ 删除操作不可恢复，请谨慎操作！</p>
      </div>

      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="删除原因" prop="delete_reason">
          <el-input
            v-model="form.delete_reason"
            type="textarea"
            :rows="4"
            placeholder="请输入删除原因（可选）"
          />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="danger" :loading="submitting" @click="handleSubmit">
        确认删除
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from "vue";
import { ElMessage, ElLoading, ElMessageBox } from "element-plus";
import { batchDeletePanAssets } from "../../../servers";

const props = defineProps({
  modelValue: { type: Boolean, default: false },
  selectedIds: { type: Array as () => number[], default: () => [] },
});

const emit = defineEmits<{
  (e: "update:modelValue", value: boolean): void;
  (e: "success"): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set: (value: boolean) => emit("update:modelValue", value),
});

const formRef = ref();
const submitting = ref(false);
const form = ref({
  delete_reason: "",
});

const rules = {
  // 删除原因是可选的
};

// 监听弹窗打开，初始化表单
watch(visible, (newVal) => {
  if (newVal) {
    form.value.delete_reason = "";
  }
});

// 提交
const handleSubmit = async () => {
  if (props.selectedIds.length === 0) {
    ElMessage.warning("请先选择要删除的资产");
    return;
  }

  // 二次确认
  try {
    await ElMessageBox.confirm(
      `确定要删除选中的 ${props.selectedIds.length} 个资产吗？此操作不可恢复！`,
      "确认删除",
      {
        confirmButtonText: "确定删除",
        cancelButtonText: "取消",
        type: "warning",
        dangerouslyUseHTMLString: false,
      }
    );
  } catch {
    return; // 用户取消
  }

  submitting.value = true;
  try {
    const params: any = {
      asset_ids: props.selectedIds,
    };

    if (form.value.delete_reason) {
      params.delete_reason = form.value.delete_reason;
    }

    const result = await batchDeletePanAssets(params);

    const deletedCount = result.data.deleted_count || 0;
    const totalCount = result.data.total_count || 0;
    ElMessage.success(`成功删除 ${deletedCount}/${totalCount} 个资产`);
    emit("success");
    emit("update:modelValue", false);
  } catch (error: any) {
    ElMessage.error(error?.message || "批量删除失败");
  } finally {
    submitting.value = false;
  }
};

// 取消
const handleCancel = () => {
  emit("update:modelValue", false);
};
</script>

<style scoped lang="scss">
.batch-delete-content {
  .info-section {
    margin-bottom: 20px;
    padding: 15px;
    background-color: #fff1f0;
    border: 1px solid #ffccc7;
    border-radius: 4px;

    p {
      margin: 5px 0;
      color: #606266;

      strong {
        color: #f5222d;
        font-size: 16px;
      }

      &.danger-text {
        color: #f5222d;
        font-size: 13px;
        margin-top: 10px;
        font-weight: 500;
      }
    }
  }
}
</style>
